﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using SosaAnalysisModule.Data;
using System.IO;

namespace SosaAnalysisModule
{
    /// <summary>
    /// Interaction logic for Replay.xaml
    /// </summary>
    public partial class Replay : UserControl, ISwitchable
    {
        private string path = "";

        public Replay()
        {
            InitializeComponent();
        }

        #region ISwitchable Members

        public void UtilizeState(object state)
        {
            throw new NotImplementedException();
        }

        #endregion

        private void chooseFile(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
            dlg.FileName = ""; // Default file name
            dlg.DefaultExt = ".csv"; // Default file extension
            dlg.Filter = "CSV documents (.csv)|*.csv"; // Filter files by extension 

            // Show save file dialog box
            Nullable<bool> result = dlg.ShowDialog();

            // Process save file dialog box results 
            if (result == false) return;

            path = dlg.FileName;
            filePathBox.Text = path;
        }
        /// <summary>
        /// //Eventlistener for Create Replay button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void createReplay(object sender, RoutedEventArgs e)
        {
            if (!File.Exists(path))
            {
                string messageBoxText = "Do you want to select a file?";
                string caption = "File Does Not Exist";
                MessageBoxButton button = MessageBoxButton.YesNo;
                MessageBoxImage icon = MessageBoxImage.Question;
                MessageBoxResult result = Xceed.Wpf.Toolkit.MessageBox.Show(messageBoxText, caption, button, icon);
                if (result == MessageBoxResult.Yes)
                    chooseFile(null, null);
                return;
            }

            SosaData data = SOSAFileReader.CreateSosaData(path);
            if (data != null)
            {
                ReplayEnviroment replayEnviro = new ReplayEnviroment(data);
                replayEnviro.Show();
            }
            else
            {
                string messageBoxText = "That may not have been the correct file";
                string caption = "Analysis Uncompleted";
                MessageBoxButton button = MessageBoxButton.OK;
                MessageBoxImage icon = MessageBoxImage.Error;
                MessageBoxResult result = Xceed.Wpf.Toolkit.MessageBox.Show(messageBoxText, caption, button, icon);
            }

        }
        //Eventlistener for back button label
        private void back_LeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Switcher.Switch(new MainMenu());
        }
        //Eventlistener for file path box text changed
        private void filePathBoxTextChanged(object sender, TextChangedEventArgs e)
        {
            path = filePathBox.Text;
        }
    }
}
